package sample.code.listing.controller;

import javax.portlet.ActionResponse;
import javax.portlet.RenderResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Validator;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.portlet.bind.annotation.ActionMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;

import sample.code.listing.domain.Book;
import sample.code.listing.utils.LongNumberEditor;
import com.teachlearnweb.service.vo.BookVO;



/**
 * AddBookController shows the add book form and handles requests
 * for adding a book to the catalog.
 * 
 * @author asarin
 *
 */
@Controller(value="addBookController")
@RequestMapping(value = "VIEW")
@SessionAttributes(types = Book.class)
public class AddBookController extends AbstractBaseController{
	
	private Logger logger = Logger.getLogger(AddBookController.class);
	
	/*@Autowired
	@Qualifier("myAddBookValidator")
	private Validator myAddBookValidator;	*/

	@ModelAttribute("book")
	public Book getCommandObject() {
		logger.info("Creating Book command object");
		return new Book();
	}

	@RenderMapping(params = "myaction=addBookForm")
	public String showAddBookForm(RenderResponse response) {
		return "addBookForm";
	}

	@InitBinder("book")
	public void initBinder(WebDataBinder binder) {
		binder.registerCustomEditor(Long.class, new LongNumberEditor());
	}

	@ActionMapping(params = "myaction=addBook")
	public void addBook(@ModelAttribute Book book,
			BindingResult bindingResult, ActionResponse response, SessionStatus sessionStatus) {
		//myAddBookValidator.validate(book, bindingResult);
		logger.info("Inside addBook action method");
		BookVO toBean = new BookVO();
		
		//bookService.addBook(book);
		BeanUtils.copyProperties(toBean, book);
		serviceLocator.getBookService().addBook(toBean);
		
		response.setRenderParameter("myaction", "books");
		//--set the session status as complete to cleanup the model attributes
		//--stored using @SessionAttributes, otherwise when you click
		//--'Add Book' button you'll see the book information pre-populated
		//-- because the getCommandObject method of the controller is not
		//--invoked
		sessionStatus.setComplete();
	}
}